﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Events;
using System.ServiceModel.DomainServices.Client;
using UI.Web.SL.Demo.Infrastructure.Events;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.RIAServices.Web;

namespace UI.Web.SL.Demo.Modules.Log.Services
{
    [Export(typeof(LogServices))]
    public class LogServices
    {
        //private InvokeOperation _addOperation;
        private IEventAggregator _eventAggregator;
        private LogDContext _logDContext = new LogDContext();

        [ImportingConstructor]
        public LogServices(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<LoggingEvent>().Subscribe(AddLog);
        }
        
        public void AddLog(string logContent)
        {

            var log = new LOG { LOGTIME = DateTime.Now, CONTENT = logContent };
            _logDContext.LOGs.Add(log);
            _logDContext.SubmitChanges(AddLogCompleted, null);
        }

        private void AddLogCompleted(SubmitOperation operation)
        {
            if (operation.IsComplete)
                _eventAggregator.GetEvent<LoggingCompletedEvent>().Publish(null);
        }
    }
}
